require "pp"
class RoutesController < ApplicationController
  # GET /routes
  # GET /routes.xml
  def index
    @sort   = ["Name", "Geschraubt", "Grad", "Umlenker", "Farbe", "Bauarbeiter"]
    if request.post?
      case params[:sort]
        when "Name"
          @routes = Route.all :order => "name"
        when "Geschraubt"
          @routes = Route.all :order => "build"
        when "Grad"
          @routes = Route.all_by_grades
        when "Umlenker"
          @routes = Route.all :order => "chain_id"
        when "Farbe" 
          @routes = Route.all :order => "color_id"
        when "Bauarbeiter"
          @routes = Route.all_by_bauarbeiter
        else
          @routes = Route.all 
      end
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @routes }
      end
    else
      @routes = Route.all.sort

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @routes }
      end
    end
  end

  # GET /routes/1
  # GET /routes/1.xml
  def show
    @route = Route.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @route }
    end
  end

  # GET /routes/new
  # GET /routes/new.xml
  def new
    @route = Route.new
    get_all_grades
    get_all_chains
    get_all_workers
    get_all_colors
  
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @route }
    end
  end

  # GET /routes/1/edit
  def edit
    @route = Route.find(params[:id])
    check_hax0r(@route)

    get_all_grades
    get_all_chains
    get_all_workers
    get_all_colors
  end

  # POST /routes
  # POST /routes.xml
  def create
    get_all_grades
    get_all_chains
    get_all_workers
    get_all_colors

    @route = Route.new(params[:route])
    
   get_all_workers

   if !params[:route][:workers_list] || params[:route][:workers_list].size == 0
     @route.errors << "Bitte mindestens einen Bauarbeiter auswaehlen!" 
   end

    respond_to do |format|
      if params[:route][:workers_list] && params[:route][:workers_list].size > 0 && @route.save
        create_route_event(@route)
        format.html { redirect_to(@route, :notice => 'Route was successfully created.', :action=>"index") }
        format.xml  { render :xml => @route, :status => :created, :location => @route }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @route.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /routes/1
  # PUT /routes/1.xml
  def update
    @route = Route.find(params[:id])
    check_hax0r(@route)

    get_all_workers

    respond_to do |format|
      if @route.update_attributes(params[:route])
        format.html { redirect_to(@route, :notice => 'Route was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @route.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /routes/1
  # DELETE /routes/1.xml
  def destroy
    @route = Route.find(params[:id])
    check_hax0r(@route)

    @route.events.each do |event|
      event.destroy
    end

    @route.destroy

    respond_to do |format|
      format.html { redirect_to(routes_url) }
      format.xml  { head :ok }
    end
  end

  def postComment
    if request.post?
      if params[:kommentar] && params[:route]
        comment = Comment.new
        comment.worker = @user
        comment.comment = params[:kommentar][:comment]
	@route = Route.find(params[:route])
        comment.routes   << Route.find(params[:route])
        comment.save

        create_comment_event(@user, @route)
      end
     respond_to do |format|
        format.html { redirect_to(@route, :notice => 'Comment was successfully added.', :action=>"index") }
      end

     end
  end
private
  def check_hax0r(route)
    if !@user.is_schorsch? && !route.workers.include?(@user)
      flash[:notice] = "Philip du spack0!"
      redirect_to "/login/logout"
    end
  end
end
